import 'dart:convert';
import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:pcp/common/config/config.dart';
import 'package:shared_preferences/shared_preferences.dart';

class ReviewPage extends StatefulWidget {
  const ReviewPage({Key key}) : super(key: key);

  @override
  _ReviewPageState createState() => _ReviewPageState();
}

class _ReviewPageState extends State<ReviewPage> {
  var mediaIdList = [];
  var mediaList = [];
  String str = "1,1111";
  var dio = Dio();
  var img;

  Future<dynamic> getReview() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String reviewId = prefs.getString("reviewId");
    try {
      Response response =
          await dio.post(Config.BASE_URL + "/review/getReview", data: {
        "reviewId": reviewId,
      });
      mediaIdList = response.data["mediaId"];
      // print(mediaIdList);
      var response2 = await dio.post(Config.BASE_URL + "/media/getReviewMedia",
          data: {"id": mediaIdList});
      mediaList = response2.data;
      // print(mediaList);
      str = 'data:image/jpeg;base64,' + mediaList[0]["buffer"];

      var response3 = await dio.get(
        Config.BASE_URL + "/media/picture/getPicture",
        queryParameters: {
          "fileId": mediaList[0]["fileId"],
          "type": mediaList[0]["type"].split('/')[1]
        },
        options: Options(
          responseType: ResponseType.stream,
        ),
        onReceiveProgress: (num received, num total) {
          double _process = double.parse('${(received / total).toStringAsFixed(2)}');/// 获取下载进度
          print(_process);
        },
      );
      final stream = await (response3.data as ResponseBody).stream.toList();
      final result = BytesBuilder();
      for (Uint8List subList in stream) {
        result.add(subList);
      }
      img = result.takeBytes();
      // print(response3.data);
      // Response<ResponseBody> rs;
      // rs = await dio.get<ResponseBody>(
      //   Config.BASE_URL + "/media/picture/getPicture",
      //   queryParameters: {
      //     "fileId": mediaList[0]["fileId"],
      //     "type": mediaList[0]["type"].split('/')[1]
      //   },
      //   options: Options(responseType: ResponseType.bytes),  //设置接收类型为stream
      // );
      // img = rs.data.stream;
      // print(rs.data.stream); //响应流
      return 1;
    } catch (e) {
      print(e);
    }
  }

  @override
  void initState() {
    super.initState();
    getReview().then((data) {
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    // PopupMenu.context = this.context;
    return new Scaffold(
        // appBar: new AppBar(
        //   elevation: 0.0,
        //   centerTitle: true,
        //   title: new Text(
        //     '我的任务列表',
        //     style: new TextStyle(fontSize: 20.0, color: Colors.white),
        //   ),
        // ),
        body: new Center(
          child: img == null
              ?  SizedBox(
            width: 1000,
            child: Center(
              child: CircularProgressIndicator(color: Colors.white),
            ),
          )
              : Image.memory(img),
        ));
  }
}
